import { getTrainTask } from '../../api/train'

Page({
  /**
   * 页面的初始数据
   */
  data: <any>{
    detail: {
      name: '',
      questionVo: {}
    },
    isShow: false,
    scores: [],
    showRejectModal: false,
    showFinishModal: false,
    showRejectedModal: false,
    showFinishedModal: false,
    showRejectInputModal: false,
    tips: ['内容有误', '内容不精准', '缺乏可操作性', '共情性低'],
    currentTipIndex: -1,
    rejectReason: '',
    form: null,
    title: '',
    content: '',
    editFlag: {
      answer: false,
      explain: false,
      name: false,
      disease: false,
      change: false,
      diagnosisTime: false,
      hospital: false,
      treatment: false,
      programme: false
    },
  },

  showEditor(e){
    const { flag } = e.currentTarget.dataset;
    const editFlag: any = {};
    editFlag[flag] = true;
    this.setData({
      editFlag: editFlag
    })
  },

  backToCase(){
    wx.navigateBack({
      delta: 1 
    });
  },

  onLoad() {
    const _self = this;
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      if(data){
        getTrainTask(data.id).then(res => {
          if(res.data){
            const list = res.data.scoreRuleList
            let scores = list && list.map(item => item.score)

            if(!scores || scores.length === 0){
              scores = new Array(list.length).fill(0)
            }

            _self.setData({
              detail: {
                ...res.data
              },
              scores
            })
          }
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    try {
      this.data.requestTask.abort()
    } catch (error) {
      console.log('还没发请求就退出了，正常不会有影响')
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      "title": "",
      "path": `pages/home/home?active=1`,
      "imageUrl": "/static/img/home.jpg"
    }
  }
})